package com.education.platform.management.controller.sys;

import com.education.platform.common.util.BeanUtil;
import com.education.platform.management.service.LoginService;
import com.education.platform.management.service.entity.LoginSuccessDTO;
import com.education.platform.management.vo.request.LoginRequestVO;
import com.education.platform.management.vo.request.UpdatePwdVO;
import com.education.platform.management.vo.response.LoginResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;

/**
* describe: 后台用户登录
* @author : tomato
* @date: 2020-06-03 09:48:32.966
**/
@RestController
@Api("用户登录")
@RequestMapping("userLogin")
@Validated
public class UserLoginController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserLoginController.class);


	@Autowired
    private LoginService loginService;


    @ApiOperation(value = "登录",notes="登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public LoginResponseVO login(@RequestBody LoginRequestVO loginRequestVO) {
        LoginSuccessDTO success = loginService.login(loginRequestVO.getUserName(),
                loginRequestVO.getPassWord());
        LoginResponseVO result = BeanUtil.convert(success, LoginResponseVO.class);
        //菜单由前端判断处理
        result.setMenuVOS(Collections.emptyList());
        return result;
    }




    @ApiOperation(value = "退出登录",notes="退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public void logout(HttpServletRequest request) {
        loginService.logout();
    }

    @ApiOperation(value = "修改密码",notes="修改密码")
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public void updatePwd(@RequestBody @Valid UpdatePwdVO updatePwdVO) {
        loginService.updatePwd(updatePwdVO.getOldPassword(),updatePwdVO.getNewPassword());
    }

}
